package ui.cli.console;

import common.Commands;
import common.commands.CommandValue;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Terminal {

	private static String prompt = "Clac>";

	private static String systemPrompt = "$>";

	public static String getPrompt() {
		return prompt;
	}

	public static String getSystemPrompt() {
		return systemPrompt;
	}

	
	
	public static void Launch() {

		CommandValue ret;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		try {
			System.out.print(prompt);
			do {

				String s = br.readLine();
				ret = Commands.RunCommand(s, false);

			} while (ret != CommandValue.EXIT);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean Launch(FileInputStream file) {

		BufferedReader br = new BufferedReader(new InputStreamReader(file));

		String s;
		CommandValue ret;

		try {
			do {
				ret = CommandValue.UNKNOWN;
				s = br.readLine();
				if (s != null && s.trim().length() > 0) {

					ret = Commands.RunCommand(s, true);
					if (ret == CommandValue.EXIT) {
						return false;
					}
					if (ret == CommandValue.UNKNOWN) {
						System.out.println("Unknown command: " + s);
					}
				} else if (s == null) {
					ret = CommandValue.EXIT;
				}
			} while (ret != CommandValue.EXIT);
		} catch (IOException e) {
			System.out.println("Error while reading script file.");
		}

		return true;
	}
}
